#pragma once

#include "wmsdk.h"
#include "stdafx.h"
#include "AutoPtr.h"
#include "FrameRate.h"

namespace ManagedVC1
{
    ref class AsfFileWriter
    {
    public:
        AsfFileWriter();
        ~AsfFileWriter()
        {
            SAFE_RELEASE(m_WMWriter);
            SAFE_RELEASE(m_WMWriterAdvanced);
            SAFE_RELEASE(m_WMProfileManager);
            SAFE_RELEASE(m_Profile);
        }

        void Initialize(System::String^ fileName, System::String^ profileFileName, BYTE* privCodecData, unsigned long privCodecDataLen, WAVEFORMATEX* pwfx);
        void WriteVideoFrame(BYTE* vc1Data, unsigned long vc1DataSize, __int64 timeStamp, FrameType ft);
        void WriteAudioFrame(BYTE* audioData, unsigned long audioSize, __int64 timeStamp);
        void Stop();

        property System::Drawing::Size^ VideoSize;
        property unsigned long VC1Profile;
        property unsigned long FrameSizeInBytes;
        property bool IsVBR;
        property double Bitrate;
        property int BufferInMilliseconds;
        property CommonFrameRate^ FrameRate;

    private:
        IWMWriter* m_WMWriter;
        IWMWriterAdvanced* m_WMWriterAdvanced;
        IWMProfileManager* m_WMProfileManager;
        IWMProfile* m_Profile;
        unsigned short m_currentInputNumber, m_videoInputNumber, m_audioInputNumber;
        bool m_audioPresent, m_videoPresent;
        static unsigned short AudioStreamNumber = 1;
        static unsigned short VideoStreamNumber = 2;

        void CreateEmptyProfile();
        void AddVideoStream(BYTE* privCodecData, unsigned long privCodecDataLen);
        void AddAudioStream(WAVEFORMATEX *pWaveLimits);
        void SetStreamBasics(IWMStreamConfig* pIWMStreamConfig, DWORD dwBitrate, WM_MEDIA_TYPE* pmt);
        void SetCodecInfo();
        void CreateWMFObjects();
        void LoadCustomProfile(System::String^ profileFile, IWMProfile** ppProfile);
    };
}